﻿Imports CodeMatrix.Context
Imports System.Linq
Imports System.Data.SqlClient
Imports CodeMatrix.Utilities
Imports System.Data

Partial Class Apps_Shares_Uc_department_fv
    Inherits System.Web.UI.UserControl

    Public DepartmentID As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not IsPostBack Then
                InitCtrl()
            End If
            DepartmentID = fv.FindControl("txtDepartmentId").ClientID
            ' Manually register the event-handling method for the  
            ' ServerValidate event of the CustomValidator control.
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Sub InitCtrl()
        Dim BreadCrumpBG As HtmlControl = Page.Master.FindControl("tdBreadCrump")
        Dim BreadCrump As Literal = Page.Master.FindControl("ltrBreadCrump")
        'Change mode by querystring
        If Not String.IsNullOrEmpty(Request.QueryString("ReadOnly")) Then
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 5)
            fv.ChangeMode(FormViewMode.ReadOnly)
        ElseIf Not String.IsNullOrEmpty(Request.QueryString("Insert")) Then
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 6)
            fv.ChangeMode(FormViewMode.Insert)
        ElseIf Not String.IsNullOrEmpty(Request.QueryString("Edit")) Or String.IsNullOrEmpty(Request.QueryString("Update")) Then
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 7)
            fv.ChangeMode(FormViewMode.Edit)
        End If
    End Sub

    Protected Sub ddlDepartmentLevel_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            '
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Protected Sub fv_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs) Handles fv.ItemInserted
        Try
            e.ExceptionHandled = True
            If e.Exception Is Nothing Then
                ' log insert
                GenericHelper.GenerateScript(GenerateScriptLogType.Insert, LogMenuType.stp_department, e.Values)
                GenericHelper.ShowAlertSuccessful(Me)
            Else
                Throw New Exception(e.Exception.Message)
            End If
        Catch ex As Exception
            e.KeepInInsertMode = True
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Protected Sub fv_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles fv.ItemInserting
        Try
            ' Server validation ---------------------------
            If e.Values("department_name").ToString().Trim() = "" Then
                Throw New Exception("กรุณากรอกชือหน่วยงาน")
            End If

            If e.Values("department_parent") = 0 Then
                e.Values("department_parent") = System.DBNull.Value
            End If
            ' Server validation ---------------------------
        Catch ex As Exception
            e.Cancel = True
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Protected Sub fv_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdatedEventArgs) Handles fv.ItemUpdated
        Try
            e.ExceptionHandled = True
            If e.Exception Is Nothing Then
                ' log update
                GenericHelper.GenerateScript(GenerateScriptLogType.Update, LogMenuType.stp_department, e.NewValues, e.OldValues)
                'Redirect to list page
                Response.Redirect("department_list.aspx?Edit=true")
            Else
                Throw New Exception(e.Exception.Message)
            End If
        Catch ex As Exception
            e.KeepInEditMode = True
            If e.Exception.Message.Contains("Cannot insert duplicate key row in object 'dbo.stp_department' with unique index 'ix_stp_department'") Then
                GenericHelper.ShowAlert(Me, "หน่วยงานนี้มีอยู่ในระบบแล้ว")
            Else
                GenericHelper.ShowAlert(Me, ex.Message)
            End If
        End Try
    End Sub

    Protected Sub fv_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles fv.ItemUpdating
        Try
            ' Server validation ---------------------------
            If e.NewValues("department_name").ToString().Trim() = "" Then
                Throw New Exception("กรุณากรอกชือหน่วยงาน")
            End If

            If e.NewValues("department_parent") = 0 Then
                e.NewValues("department_parent") = System.DBNull.Value
            End If
            ' Server validation ---------------------------
        Catch ex As Exception
            e.Cancel = True
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub
End Class
